/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DNNspot.LiveChat.DataModel;
using DotNetNuke.Common;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;

namespace DNNspot.LiveChat
{
    public partial class ClientChat : ChatPageBase
    {
        // ### PRIVATE MEMBERS #############################################

        private Boolean _agentsAreAvailabe;        

        // ### EVENTS ######################################################

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (_agentsAreAvailabe)
            {
                IncludeJS();

                int? moduleId = null;
                if(Request.Params["mid"] != null)
                {
                    moduleId = Convert.ToInt32(Request.Params["mid"]);
                }
                if (moduleId.HasValue && moduleId > 0)
                {
                    ModuleController moduleController = new ModuleController();
                    ModuleInfo moduleInfo = moduleController.GetModule(moduleId.Value);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryCollection.Initialize(PortalId);
            AgentCollection.RemoveAllInactive(90);
            _agentsAreAvailabe = AgentCollection.AgentIsAvailable(PortalId);

            if (!IsPostBack)
            {
                if (_agentsAreAvailabe)
                    LoadControls();
                else
                {
                    lblMessage.Text = "There are no agents available at this time.";

                    lblMessage.Visible = true;
                    divQuestions.Visible = false;
                    divTranscript.Visible = false;
                    divInput.Visible = false;
                }
            }
        }

        // ### PRIVATE FUNCTIONS ###########################################

        private void IncludeJS()
        {
            if (string.IsNullOrEmpty(HostSettings.GetHostSetting(ModuleSettings.NoJQuery)) && HttpContext.Current.Items["jquery_registered"] == null)
            {
                RegisterJavascript(String.Format("{0}{1}js/jquery-1.3.2.min.js", Globals.ApplicationPath, ChatPageWebPath));
                HttpContext.Current.Items.Add("jquery_registered", "true");
            }

            RegisterJavascript(string.Format("{0}{1}js/dnnspot.livechat.client.include.js", Globals.ApplicationPath, ChatPageWebPath));

            RegisterJavascript(string.Format("{0}{1}js/jquery.layout.js", Globals.ApplicationPath, ChatPageWebPath));
            RegisterJavascript(string.Format("{0}{1}js/jquery.validationEngine.js", Globals.ApplicationPath, ChatPageWebPath));
            RegisterJavascript(string.Format("{0}{1}js/jquery.validationEngine-en.js", Globals.ApplicationPath, ChatPageWebPath));

            RegisterJavascript(string.Format("{0}{1}js/dnnspot.livechat.client.js", Globals.ApplicationPath, ChatPageWebPath));
        }

        private void LoadCategoryDropDownList()
        {
            var categoryQuery = new CategoryQuery();
            categoryQuery.Where(categoryQuery.PortalId == PortalId);

            var categories = new CategoryCollection();
            categories.Load(categoryQuery);

            foreach (Category category in categories.SortByParentTree())
                ddlCategories.Items.Add(new ListItem(category.GetTreeName(),category.CategoryId.ToString()));
        }

        private void LoadControls()
        {
            LoadCategoryDropDownList();

            var clientId = Request.Cookies[Cookies.ClientId] != null ? Convert.ToInt32(Request.Cookies[Cookies.ClientId].Value) : -1;
            var client = new Client();

            hidChatPageWebPath.Value = ChatPageWebPath;
            hidClientId.Value = clientId.ToString();
            hidIp.Value = Request.UserHostAddress;
            hidPortalId.Value = PortalId.ToString();
            hidUrlReferrer.Value = Convert.ToString(Request.UrlReferrer);
            hidUserId.Value = UserId.ToString();

            if (client.LoadByPrimaryKey(clientId))
            {
                hidEmail.Value = client.Email;
                hidFirstName.Value = client.FirstName;
                hidLastName.Value = client.LastName;
            }
        }
    }
}
